using System;
using System.Collections.Generic;

namespace ListExample // 命名空间
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // 创建一个 List
            List<string> names = new List<string>();

            // 添加元素到 List
            names.Add("Alice");
            names.Add("Bob");
            names.Add("Charlie");

            // 打印 List 的元素
            Console.WriteLine("List of Names:");
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }

            // 插入元素
            names.Insert(1, "David"); // 在索引 1 处插入 "David"

            Console.WriteLine("\nAfter inserting a new name:");
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }

            // 替换元素
            names[2] = "Eve"; // 将 "Charlie" 替换为 "Eve"

            Console.WriteLine("\nAfter replacing an element:");
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }

            // 删除元素
            names.Remove("Bob"); // 删除 "Bob"

            Console.WriteLine("\nAfter removing an element:");
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }

            // 获取元素的数量
            Console.WriteLine($"\nTotal names in List: {names.Count}");

            // 清空 List
            names.Clear();
            Console.WriteLine($"Total names after clearing: {names.Count}"); // 输出: 0
        }
    }
}
